package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.MyCertificateEntity;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.tool.ScreenShotHelper;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.rxnetlibrary.utils.ToastUtils;
//import com.umeng.socialize.ShareAction;
//import com.umeng.socialize.UMShareListener;
//import com.umeng.socialize.bean.SHARE_MEDIA;
//import com.umeng.socialize.media.UMImage;
//import com.umeng.socialize.media.UMWeb;

public class MyCertificatePoppupWindow extends PopupWindow {
    private ImageView iv_dismiss, iv_certificate_img;
    private RoundedCornersImage rci_user_header, rvi_companyLogo;
    private TextView tv_title_name, tv_details, tv_user_name, tv_company_name;
    private Context context;
    private View layout;
    private String nickname;
    private String headImage;
    private String companyName;
    private String companyLogo;
    private MyCertificateEntity.DataBean.CertificatesBean certificatesBean;
    private RelativeLayout rl_content,rl_bg;

    public OnClickListenr getOnClickListenr() {
        return onClickListenr;
    }

    public void setOnClickListenr(OnClickListenr onClickListenr) {
        this.onClickListenr = onClickListenr;
    }

    private OnClickListenr onClickListenr;

    public static MyCertificatePoppupWindow getInstance(Context context, String nickname, String headImage,
                                                        String companyLogo, String companyName,
                                                        MyCertificateEntity.DataBean.CertificatesBean certificatesBean) {
        return new MyCertificatePoppupWindow(context, nickname, headImage, companyLogo, companyName, certificatesBean);
    }

    private MyCertificatePoppupWindow(Context context, String nickname, String headImage,
                                      String companyLogo, String companyName,
                                      MyCertificateEntity.DataBean.CertificatesBean certificatesBean) {
        super(context);
        this.context = context;
        this.nickname = nickname;
        this.headImage = headImage;
        this.companyName = companyName;
        this.companyLogo = companyLogo;
        this.certificatesBean = certificatesBean;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);
        initEvent();
    }


    private void initView() {
        layout = View.inflate(context, R.layout.popup_my_certificate, null);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        iv_certificate_img = layout.findViewById(R.id.iv_certificate_img);
        tv_details = layout.findViewById(R.id.tv_details);
        rci_user_header = layout.findViewById(R.id.rci_user_header);
        rvi_companyLogo = layout.findViewById(R.id.rvi_companyLogo);
        tv_user_name = layout.findViewById(R.id.tv_user_name);
        tv_company_name = layout.findViewById(R.id.tv_company_name);
        rl_content = layout.findViewById(R.id.rl_content);
        rl_bg=layout.findViewById(R.id.rl_bg);
        tv_user_name.setText(nickname);
        GlideUtils.loadImage(context, headImage, rci_user_header);
        GlideUtils.loadImage(context, companyLogo, rvi_companyLogo);
        tv_company_name.setText(companyName);
        if (certificatesBean != null) {
            String details = "恭喜你参与完成了<font color='#333333'>%s</font>“我们的长征路”活动 <font color='#333333'>%s</font>挑战！";
            tv_details.setText(Html.fromHtml(String.format(details, companyName, certificatesBean.sectionName)));
            GlideUtils.loadImage(context, certificatesBean.medalUrl, iv_certificate_img);
        }

        rl_content.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (ScreenShotHelper.saveScreenShot(context,rl_content) == 1) {
                    ToastUtils.showShort("保存成功");
                }
                return false;
            }
        });

    }

    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.4f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public interface OnClickListenr {
        void onCancelClick();

        void onSureClick();
    }
}
